<?php
/**
 * DolphinGTK - phpGtk framework
 *
 * @package    DolphinGTK
 * @author     Askarov Ruslan
 * @copyright  Askarov Ruslan (c) 2010.
 * @link       http://groups.google.com/group/dolphingtk
 * @license    http://www.opensource.org/licenses/gpl-license.php GPL
 * @version    0.1
 * @since      PHP v.: 5.2.1
 * @subpackage desktop
 * @category   functions
 * @filesource
 */



/**
 * Функция для работы с gettext
 *
 * @param  string $string
 * @return string
 */
function i18n( $string )
{
  // проверим наличие
  if( empty($string) ) {
    print "Enter text value\n";
    return false;
  }

  // получаем аргументы
  $args = func_get_args();
  array_shift($args);

  // проверим наличие функции gettext
  if( function_exists('gettext') )
  {
    // если есть аргументы, то и их постараемся перевести
    if( is_array($args) && count($args) >= 1 )
    {
      $args = array_map('gettext', $args);
    }
    elseif( is_array($args) && count($args) == 0 )
    {
      return gettext($string);
    }
    // переводим так же и строчку
    $string = gettext($string);
  }
  else
  {
    return vsprintf($string, $args);
  }
  // отдаём результат
  return vsprintf($string, $args);
}




/**
 * Функция для загрузки библиотек
 *
 * @param  string $file
 * @return void
 */
function load( $file )
{
  if( !empty($file) ) {
    $file = strtolower($file);
    $file = BASE_DIR . 'library/'.$file.'.php';
    if( file_exists($file) ) {
      include_once $file;
    } else {
      print "Lib file '" .$file. ''. "' not exists.\n";
    }
  } else {
    print "Enter lib file name.\n";
    exit;
  }
}




/**
 * Функция для определения ОС
 *
 * @return string
 */
function get_os()
{
  if( stristr(PHP_OS, 'winnt') or stristr(PHP_OS, 'win32') ) {
    return 'windows';
  } elseif( stristr(PHP_OS, 'lin') ) {
    return 'linux';
  } elseif (stristr(PHP_OS, 'darwin') or stristr(PHP_OS, 'mac')) {
    return 'macos';
  } else {
    return 'unknown';
  }
}


// На всякий случай резерв. Мало ли.
$GLOBALS['GTK_ICON_RES'] = array(
  'about'                  => Gtk::STOCK_ABOUT,
  'add'                    => Gtk::STOCK_ADD,
  'apply'                  => Gtk::STOCK_APPLY,
  'bold'                   => Gtk::STOCK_BOLD,
  'cancel'                 => Gtk::STOCK_CANCEL,
  'cdrom'                  => Gtk::STOCK_CDROM,
  'clear'                  => Gtk::STOCK_CLEAR,
  'close'                  => Gtk::STOCK_CLOSE,
  'color_picker'           => Gtk::STOCK_COLOR_PICKER,
  'connect'                => Gtk::STOCK_CONNECT,
  'convert'                => Gtk::STOCK_CONVERT,
  'copy'                   => Gtk::STOCK_COPY,
  'cut'                    => Gtk::STOCK_CUT,
  'delete'                 => Gtk::STOCK_DELETE,
  'dialog_authentication'  => Gtk::STOCK_DIALOG_AUTHENTICATION,
  'dialog_error'           => Gtk::STOCK_DIALOG_ERROR,
  'dialog_info'            => Gtk::STOCK_DIALOG_INFO,
  'dialog_question'        => Gtk::STOCK_DIALOG_QUESTION,
  'dialog_warning'         => Gtk::STOCK_DIALOG_WARNING,
  'directory'              => Gtk::STOCK_DIRECTORY,
  'disconnect'             => Gtk::STOCK_DISCONNECT,
  'dnd'                    => Gtk::STOCK_DND,
  'dnd_multiple'           => Gtk::STOCK_DND_MULTIPLE,
  'edit'                   => Gtk::STOCK_EDIT,
  'execute'                => Gtk::STOCK_EXECUTE,
  'file'                   => Gtk::STOCK_FILE,
  'find'                   => Gtk::STOCK_FIND,
  'find_and_replace'       => Gtk::STOCK_FIND_AND_REPLACE,
  'floppy'                 => Gtk::STOCK_FLOPPY,
  'go_back'                => Gtk::STOCK_GO_BACK,
  'go_down'                => Gtk::STOCK_GO_DOWN,
  'go_forward'             => Gtk::STOCK_GO_FORWARD,
  'go_up'                  => Gtk::STOCK_GO_UP,
  'goto_bottom'            => Gtk::STOCK_GOTO_BOTTOM,
  'goto_first'             => Gtk::STOCK_GOTO_FIRST,
  'goto_last'              => Gtk::STOCK_GOTO_LAST,
  'goto_top'               => Gtk::STOCK_GOTO_TOP,
  'harddisk'               => Gtk::STOCK_HARDDISK,
  'help'                   => Gtk::STOCK_HELP,
  'home'                   => Gtk::STOCK_HOME,
  'indent'                 => Gtk::STOCK_INDENT,
  'index'                  => Gtk::STOCK_INDEX,
  'italic'                 => Gtk::STOCK_ITALIC,
  'jump_to'                => Gtk::STOCK_JUMP_TO,
  'justify_center'         => Gtk::STOCK_JUSTIFY_CENTER,
  'justify_fill'           => Gtk::STOCK_JUSTIFY_FILL,
  'justify_left'           => Gtk::STOCK_JUSTIFY_LEFT,
  'justify_right'          => Gtk::STOCK_JUSTIFY_RIGHT,
  'media_forward'          => Gtk::STOCK_MEDIA_FORWARD,
  'media_next'             => Gtk::STOCK_MEDIA_NEXT,
  'media_pause'            => Gtk::STOCK_MEDIA_PAUSE,
  'media_play'             => Gtk::STOCK_MEDIA_PLAY,
  'media_previous'         => Gtk::STOCK_MEDIA_PREVIOUS,
  'media_record'           => Gtk::STOCK_MEDIA_RECORD,
  'media_rewind'           => Gtk::STOCK_MEDIA_REWIND,
  'media_stop'             => Gtk::STOCK_MEDIA_STOP,
  'missing_image'          => Gtk::STOCK_MISSING_IMAGE,
  'network'                => Gtk::STOCK_NETWORK,
  'new'                    => Gtk::STOCK_NEW,
  'no'                     => Gtk::STOCK_NO,
  'ok'                     => Gtk::STOCK_OK,
  'open'                   => Gtk::STOCK_OPEN,
  'paste'                  => Gtk::STOCK_PASTE,
  'preferences'            => Gtk::STOCK_PREFERENCES,
  'print'                  => Gtk::STOCK_PRINT,
  'print_preview'          => Gtk::STOCK_PRINT_PREVIEW,
  'properties'             => Gtk::STOCK_PROPERTIES,
  'quit'                   => Gtk::STOCK_QUIT,
  'redo'                   => Gtk::STOCK_REDO,
  'refresh'                => Gtk::STOCK_REFRESH,
  'remove'                 => Gtk::STOCK_REMOVE,
  'revert_to_saved'        => Gtk::STOCK_REVERT_TO_SAVED,
  'save'                   => Gtk::STOCK_SAVE,
  'save_as'                => Gtk::STOCK_SAVE_AS,
  'select_color'           => Gtk::STOCK_SELECT_COLOR,
  'select_font'            => Gtk::STOCK_SELECT_FONT,
  'sort_ascending'         => Gtk::STOCK_SORT_ASCENDING,
  'sort_descending'        => Gtk::STOCK_SORT_DESCENDING,
  'spell_check'            => Gtk::STOCK_SPELL_CHECK,
  'stop'                   => Gtk::STOCK_STOP,
  'strikethrough'          => Gtk::STOCK_STRIKETHROUGH,
  'undelete'               => Gtk::STOCK_UNDELETE,
  'underline'              => Gtk::STOCK_UNDERLINE,
  'undo'                   => Gtk::STOCK_UNDO,
  'unindent'               => Gtk::STOCK_UNINDENT,
  'yes'                    => Gtk::STOCK_YES,
  'zoom_100'               => Gtk::STOCK_ZOOM_100,
  'zoom_fit'               => Gtk::STOCK_ZOOM_FIT,
  'zoom_in'                => Gtk::STOCK_ZOOM_IN,
  'zoom_out'               => Gtk::STOCK_ZOOM_OUT
);




/**
 * Функция для получения всех иконок.
 *
 * @return bool
 */
function get_all_icons()
{
  if( !class_exists('ReflectionClass') ) {
    $GLOBALS['GTK_ICON'] = $GLOBALS['GTK_ICON_RES'];
    return false;
  }

  $reflection = new ReflectionClass('Gtk');

  foreach( $reflection->getConstants() as $const => $values )
  {
    if( strpos($const, 'STOCK') === 0 )
    {
      $key = str_replace(
        array('gtk-', '-'),
        array('', '_'),
        $values
      );
      $stock_item = Gtk::stock_lookup($values);
      $label = str_replace('_', '', $stock_item[1] );
      $GLOBALS['GTK_ICON'][$key] = array(
        $values, "Gtk::$const", $label
      );
    }
  }
  return true;
}




/**
 * Функция для проверки по слову - встроенных иконок
 *
 * @param string $icon
 * @return int
 */
function get_icon( $icon )
{
  if( !$GLOBALS['GTK_ICON'] ) {
    get_all_icons();
  }

  $icons = $GLOBALS['GTK_ICON'];

  if( is_string($icon) && key_exists( $icon, $icons ) )
  {
    return $icons[$icon][0];
  }
  else
  {
    return false;
  }
}




/**
 * Функция для сохранения информации в файл
 *
 * @param string $file
 * @param string $text
 * @param string $mode
 * @return bool
 */
function writefile( $file = false, $text = false, $mode = 'w' )
{
  $fp = @fopen ( $file, $mode );

  if( $fp && $file)
  {
    flock($fp,2);
    fwrite ( $fp, $text );
    flock($fp,3);
    fclose ( $fp );

    return true;
  } else return false;
}

// Дебагинг
if( DEBUG === false or !defined('DEBUG') ) {
  ini_set('display_errors', false);
  ini_set('log_errors', true);
} else {
  error_reporting(E_ALL | E_STRICT);
}

// Абсолютный путь
if( !defined('BASE_DIR') ) {
  define('BASE_DIR', './');
}

// Получаем все иконки
get_all_icons();
?>
